package com.Cychat.AI.controller;

import com.Cychat.commons.entity.ResultMessage;
import com.Cychat.AI.utils.OSSUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/ai/upload")
@Slf4j
@CrossOrigin
public class FileUploadController {

    @Autowired
    private OSSUtil ossUtil;

    /**
     * 上传图片接口（供前端调用）
     */
    @PostMapping("/image/{userId}")
    public ResultMessage uploadImage(@PathVariable("userId") Long userId, @RequestParam("file") MultipartFile file) {
        try {
            if (file == null || file.isEmpty()) {
                return ResultMessage.error("请选择图片文件");
            }
            
            // 上传到OSS并获取URL
            String imageUrl = ossUtil.uploadImage(file);
            return ResultMessage.success(imageUrl);
        } catch (Exception e) {
            log.error("图片上传失败", e);
            return ResultMessage.error(e.getMessage());
        }
    }
}